﻿using System;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using System.Web.Caching;

using LTJ.Common;
using LTJ.Data;
using LTJ.Config;
using LTJ.Entity;
using LTJ.Common.Generic;
using LTJ.Cache;

namespace LTJ.FrameWork
{
    /// <summary>
    /// 航线授权管理
    /// </summary>
    public class Routes
    {
        /// <summary>
        /// 创建航线授权信息
        /// </summary>
        /// <returns>-1则为航线不能重复授权</returns>
        public static int CreateRoute(RouteInfo rinfo)
        {
            if (ExistReRoute(rinfo.cid, rinfo.tcode, rinfo.bcode)) return -1;
            return Data.DataProvider.Routes.CreateRoute(rinfo);
        }

        /// <summary>
        /// 判断航线重复授权
        /// </summary>
        /// <param name="cid">公司ID</param>
        /// <param name="tcode">航线Code</param>
        /// <param name="bcode">船公司code</param>
        public static bool ExistReRoute(int cid, string tcode, string bcode)
        {
            return Data.DataProvider.Routes.ExistReRoute(cid, tcode, bcode);
        }

        /// <summary>
        /// 根据ID获取授权信息
        /// </summary>
        public static RouteInfo GetRouteByID(int id)
        {
            return Data.DataProvider.Routes.GetRountByID(id);
        }
        /// <summary>
        /// 根据条件获取授权信息
        /// </summary>
        public static RouteInfo GetRouteByStr(string strWhere)
        {
            return Data.DataProvider.Routes.GetRountByStr(strWhere);
        }

        /// <summary>
        /// 获取航线授权信息分页
        /// </summary>
        public static DataTable GetRoutesByPage(int pageindex, int pagesize, string condition, string ordercolumn, string ordertype)
        {
            return Data.DataProvider.Routes.GetRoutesByPage(pageindex, pagesize, condition, ordercolumn == "" ? "id" : ordercolumn, ordertype == "" ? "desc" : ordertype);
        }

        /// <summary>
        /// 根据条件获取航线授权信息集合数量
        /// </summary>
        public static int GetRoutesCount(string conditions)
        {
            return Data.DataProvider.Routes.GetRoutesCount(conditions);
        }

        /// <summary>
        /// 修改航线授权信息
        /// </summary>
        /// <returns>-1则为航线不能重复授权，0为修改失败</returns>
        public static int UpdateRoute(RouteInfo rinfo)
        {
            if (ExistReRoute(rinfo.cid, rinfo.tcode, rinfo.bcode)) return -1;
            return Data.DataProvider.Routes.UpdateRoute(rinfo);
        }
        public static int DelRoutes(string ids)
        {
            return Data.DataProvider.Routes.DelRoutes(ids);
        }
        public static DataTable GetRountTabByStr(string strWhere)
        {
            return Data.DataProvider.Routes.GetRountTabByStr(strWhere);
        }

        /// <summary>
        /// 获取企业授权航线集合
        /// </summary>
        public static List<RouteInfo> GetRoutesByCid(int cid)
        {
            return Data.DataProvider.Routes.GetRountList("cid=" + cid);
        }

        /// <summary>
        /// 根据企业获取航线信息集合
        /// </summary>
        public static List<RouteInfo> GetRountListByCid(int cid)
        {
            return GetRountListByCidShip(cid, "", false);
        }

        /// <summary>
        /// 根据公司及船公司获取航线信息
        /// </summary>
        /// <param name="cid">公司ID</param>
        /// <param name="shipcode">船公司代码</param>
        /// <param name="isship">是否加载船公司判断</param>
        public static List<RouteInfo> GetRountListByCidShip(int cid, string shipcode, bool isship)
        {
            string querystr = "";

            if (isship)
            {
                if (cid > 0) querystr = " cid = " + cid + " AND bcode = '" + shipcode + "'";
            }
            else
            {
                if (cid > 0 && shipcode != "") querystr = " cid = " + cid + " AND bcode = '" + shipcode + "'";
                else if (cid > 0) querystr = " cid = " + cid;
            }

            return Data.DataProvider.Routes.GetRountList(querystr);
        }

        /// <summary>
        /// 返回查询条件
        /// </summary>
        /// <param name="cid"></param>
        /// <param name="type"></param>
        /// <param name="searchname"></param>
        /// <param name="searchename"></param>
        /// <returns></returns>
        public static string GetStrWhere(int cid, int type, string searchname, string searchename)
        {
            string rWhere = "cid = " + cid;
            string sname = "name";
            string sename = "ename";

            if (type == 1)
            {
                sname = "routebname";
                sename = "routebename";
            }
            else if (type == 2)
            {
                sname = "routeseaname";
                sename = "routeseaename";
            }

            if (searchname != "") rWhere += " AND " + sname + " LIKE '%" + searchname + "%'";
            if (searchename != "") rWhere += " AND " + sename + " LIKE '%" + searchename + "%'";

            return rWhere;
        }

        /// <summary>
        /// 返回授权航线查询条件
        /// </summary>
        public static string GetRouteStrWhere(int cid, string searchname, string searchename)
        {
            return GetStrWhere(cid, 0, searchname, searchename);
        }

        /// <summary>
        /// 返回船公司查询条件
        /// </summary>
        public static string GetRouteBoatStrWhere(int cid, string searchname, string searchename)
        {
            return GetStrWhere(cid, 1, searchname, searchename);
        }

        /// <summary>
        /// 返回航线查询条件
        /// </summary>
        public static string GetRouteSeaStrWhere(int cid, string searchname, string searchename, string boatstr)
        {

            return GetStrWhere(cid, 2, searchname, searchename) + " AND bcode = '" + boatstr + "'";
        }

        public static int AddRoute(DataTable tab)
        {
            int rows = 0;

                int u_row = 0;
                DataTable dt = new DataTable();
                dt = tab.Clone();
                foreach (DataRow dr in tab.Rows)
                {

                    dt.ImportRow(dr);
                    u_row++;
                    if (u_row == 20)
                    {
                        u_row = 0;

                        rows = Data.DataProvider.Routes.AddRoute(dt);
                        dt.Rows.Clear();
                    }
                }

                if (u_row < 20 && u_row > 0)
                {
                    rows = Data.DataProvider.Routes.AddRoute(dt);
                    dt.Rows.Clear();
                }

                dt.Dispose();
                return rows;
        }
    }
}
